package com.xxwy.ddu.cms.authonrize.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xxwy.ddu.cms.authonrize.entity.Access;
import com.xxwy.ddu.cms.authonrize.entity.Role;
import com.xxwy.ddu.cms.authonrize.entity.RoleAccess;
import com.xxwy.ddu.cms.authonrize.mapper.AccessMapper;
import com.xxwy.ddu.cms.authonrize.mapper.RoleAccessMapper;
import com.xxwy.ddu.cms.authonrize.mapper.RoleMapper;
import com.xxwy.ddu.cms.authonrize.service.IRoleAccessService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxwy
 * @since 2018-09-05
 */
@Service
public class RoleAccessServiceImpl extends ServiceImpl<RoleAccessMapper, RoleAccess> implements IRoleAccessService {

    @Autowired
    RoleAccessMapper roleAccessMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AccessMapper accessMapper;

    @Override
    public void insertRoleAccess(RoleAccess roleAccess) {
        Role role = roleMapper.selectById(roleAccess.getrId());
        if (role != null) {
            Access access = accessMapper.selectById(roleAccess.getaId());
            if (access != null) {
                //判断是是不是已经存在权限
                RoleAccess roleAccess1 = getRoleAccessEntityWrapper(roleAccess);
                if (roleAccess1 == null) {
                    //没有这个权限
                    roleAccess.setAcDescription(role.getrName()+"拥有"+access.getaDescribe());
                    roleAccess.setDeleteFlag(0);
                    roleAccess.insert();
                }
            }
        }
    }


    @Override
    public void delectRoleAccess(RoleAccess roleAccess){
        RoleAccess roleAccess1 = getRoleAccessEntityWrapper(roleAccess);
        if (roleAccess1 != null) {
            roleAccessMapper.deleteById(roleAccess1.getAcId());
        }
    }

    private RoleAccess getRoleAccessEntityWrapper(RoleAccess roleAccess) {
        EntityWrapper<RoleAccess> wrapper = new EntityWrapper<>();
        wrapper.eq("a_id",roleAccess.getaId());
        wrapper.eq("r_id",roleAccess.getrId());
        RoleAccess roleAccess1 = selectOne(wrapper);
        return roleAccess1;
    }
}
